In this lesson, you learned about functions. You saw the following:
Project 7 Listing. A multifunction program that passes data between functions.
1:// Filename: PROJECT7.CPP
2:// A multifunction program that computes payroll amounts
3:#include <iostream.h>
4:
5:
6:void GetPayrollInput(char name[], float &rate, int &hours);
7:float CalcWages(float rate, int hours);
8:float CalcTaxes(float pay, float taxRate = 0.34);
9:void PrintResults(const char name[],float pay, float taxes);
10://
11:// Overloaded input functions to get individual values
12://
13:void GetInput(const char prompt[],char string[]);
14:void GetInput(const char prompt[],float& number);
15:void GetInput(const char prompt[],int& number);
16:void main()
17:{
18: int hours;
19: float rate, pay, taxes;
20: char employee[30];
21: GetPayrollInput(employee, rate, hours);// Get the input data
22: pay = CalcWages(rate, hours);// Calculate payroll results
23: taxes = CalcTaxes(pay); // Calculate payroll taxes
24: PrintResults(employee, pay, taxes); // Print the results
25: return; // Return to QuickWin
26:}
27://************************************************************
28:void GetPayrollInput(char name[], float &rate, int &hours)
29:{
30: // Code to get employee data
31: GetInput("Employee name? ",name);
32: GetInput("How many hours did this employee work? ",hours);
33: GetInput("What is the pay rate per hour? ",rate);
34: return; // Return to main()
35:}
36://************************************************************
37:void GetInput(const char prompt[], char string[])
38: {
39: cout << prompt;
40: cin.getline(string,30);
41: return;
42: }
43://************************************************************
44:void GetInput(const char prompt[], float& number)
45: {
46: cout << prompt;
47: cin >> number;
48: return;
49: }
50://************************************************************
51:void GetInput(const char prompt[], int& number)
52: {
53: cout << prompt;
54: cin >> number;
55: return;
56: }
57://************************************************************
58:float CalcWages(float rate, int hours)
59:{ // Code to calculate payroll amounts
60: return (rate * (float)hours); // Return to main()
61:}
62://************************************************************
63:float CalcTaxes(float pay, float taxRate)
64:{ // Code to compute taxes
65: return (pay * taxRate); // Return to main()
66:}
67://************************************************************
68:void PrintResults(const char name[], float pay, float taxes)
69:{ // Code to print the results
70: cout.precision(2);
71: cout.setf(ios::showpoint);
72: cout.setf(ios::fixed);
73: cout << endl << name << " earned a total of $"
74: << pay << endl;
75: cout << "Before $" << taxes << " in taxes." << endl;
76: return; // Return to main()
77:}
Output
Employee name? Keith Gutteridge How many hours did this employee work? 40 What is the pay rate per hour? 25.50 Keith Gutteridge earned a total of $1020.00 Before $346.80 in taxes.
Description
1: A C++ comment that includes the program's filename.
2: A C++ comment that contains the program's description.
3: The cout and cin commands need information in the IOSTREAM.H header file.
4: Blank lines improve your program's readability.
5: Blank lines improve your program's readability.
6: Prototype of payroll input function.
7: Prototype of wages calculation function.
8: Prototype of taxes calculation function with default parameter for tax rate.
9: Prototype of printing function.
10: Comments improve readability.
11: Comments improve readability.
12: Comments improve readability.
13: Prototype of overloaded input function, string version (employee name).
14: Prototype of overloaded input function, float version (hourly rate).
15: Prototype of overloaded input function, int version (hours).
16: main() begins.
17: All functions begin with an opening brace.
18: An integer variable that will hold the number of hours worked.
19: float variables to hold various payroll values.
20: A char array to hold the employee name.
21: Passes rate and hours by reference so that GetPayrollInput() can change them and keep those changed values in main().
22: CalcWages() uses the rate and hours figures to calculate and return the gross pay. main() captures the return value in pay.
23: CalcTaxes() uses the gross pay to calculate and return the taxes. main() captures the return value in taxes.
24: The employee, gross pay, and taxes are sent to PrintResults() to be displayed on-screen.
25: Always return from main() to QuickWin.
26: A final brace ends all main() functions.
27: A line of asterisks helps to separate functions.
28: The definition (first line) of GetPayrollInput(). Receives three parameters: name by address, rate, and hours by reference.
29: All functions begin with an opening brace.
30: Comment to explain the function.
31: Call another function to get the employee name.
32: Call another function to get hours worked.
33: Call another function to get rate per hour.
34: Return to main() having passed data back via reference parameters.
35: A final brace ends a function definition.
36: A line of asterisks helps to separate functions.
37: The definition of GetInput(), which retrieves a string. It receives a const char array so that the calling program can send a literal to prompt the user.
38: All functions begin with an opening brace.
39: Use cout to output a prompt for user input.
40: cin.getline retrieves a whole line of text up to a maximum number of characters.
41: return to the calling function, GetPayrollInput.
42: A final brace ends a function definition.
43: A line of asterisks helps to separate functions.
44: The definition of GetInput() for a float. Receives a const char array so that the calling program can send literal for prompt.
45: All functions begin with an opening brace.
46: Use cout to output a prompt for user input.
47: cin >> retrieves a float.
48: return to the calling function, GetPayrollInput.
49: A final brace ends a function definition.
50: A line of asterisks helps to separate functions.
51: The definition of GetInput() for a float. Receives a const char array so that the calling program can send literal for prompt.
52: All functions begin with an opening brace.
53: Use cout to output a prompt for user input.
54: cin >> retrieves a float.
55: return to the calling function, GetPayrollInput.
56: A final brace ends a function definition.
57: A line of asterisks helps to separate functions.
58: Defines the function that calculates gross pay. This function receives a floating-point and an integer parameter passed by value.
59: Scatter comments throughout your code.
60: Returns the wages (the number of hours multiplied by the rate per hour).
61: A final brace ends all functions.
62: A line of asterisks helps to separate functions.
63: Defines the function that calculates taxes. Requires a float and the tax rate both passed by value, which can be defaulted (line 6).
64: Scatter comments throughout your code.
65: Returns the taxes to main(), which are simply the gross pay multiplied by the tax rate.
66: A final brace ends all functions.
67: A line of asterisks helps to separate functions.
68: Defines the function that prints the results, and requires a char array passed by address and two parameters passed by value.
69: Scatter comments throughout your code.
70: Ensure that two decimal places print.
71: Always show the decimal point.
72: Guard against scientific notation.
73: Prints the employee's gross pay.
74: The long cout is concluded.
75: Prints the employee's tax requirement.
76: Returns to main().
77: A final brace ends all functions.
6: Prototype all functions.
8: The tax rate is defaulted if rate is not present.
13: C++ distinguishes different overloaded functions by parameter data types.
16: Keep main() as simple as possible.
21: The function receives the variables by reference.
23: The tax rate parameter is not specified; therefore, the default is used.
31: Break down a complicated function into several easier steps by using function calls.
37: The passing of the prompt means that the function could be reused to get other values in the program.
44: By breaking down the function, each step is small and easy to understand.
58: You can return at most one value.